﻿using API;
using API.Store;
using Common;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class ProviderBLL : BaseBLL
    {
        public void BatchInsert(DateTime? lastUpdateDate = null)
        {
            try
            {
                this.Request.LastUpdateDate = lastUpdateDate;
                ClientRequest request=this.StoreRequest();
                ApiStore dataProxy = ServiceProxy.ApiStoreProxy();
                MB_Provider[] providers = dataProxy.GetProviders(request);
                if (providers == null || providers.Length <= 0) return;
                using (UnitOfWork uow = DatabaseSchema.UnitOfWork)
                {
                    foreach (MB_Provider p in providers)
                    {
                        B_Provider provider = new B_Provider(uow);
                        provider.ProviderID = p.ProviderID;
                        provider.ProviderCode = p.ProviderCode;
                        provider.ProviderName = p.ProviderName;
                        provider.StoreID = p.StoreID;
                        provider.OperatTypeID = p.OperatTypeID;
                        provider.Sort = p.Sort;
                        provider.Creator = p.Creator;
                        provider.CreateDT = p.CreateDT;
                        provider.Operator = p.Operator;
                        provider.UpdateDT = p.UpdateDT.Value;
                        provider.Remark = p.Remark;
                        provider.Save();
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ProviderBLL.BatchInsert", ex);
            }
        }
        public DateTime MaxUpdateDate()
        {
            DateTime updateDT = DateTime.MinValue;
            string sql = "select Max(UpdateDT) from B_Provider";
            updateDT = Convert.ToDateTime(DatabaseSchema.UnitOfWork.ExecuteScalar(sql));
            return updateDT;
        }
    }
}
